package com.emergency.embackend.controller;

import com.emergency.embackend.common.PageParams;
import com.emergency.embackend.common.R;
import com.emergency.embackend.entity.HelpComment;
import com.emergency.embackend.entity.HelpData;
import com.emergency.embackend.entity.HelpHot;
import com.emergency.embackend.entity.ShareComment;
import com.emergency.embackend.exception.EmException;
import com.emergency.embackend.service.HelpDataService;
import com.emergency.embackend.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/help")
public class HelpDataController {
    @Autowired
    private HelpDataService helpDataService;
    //添加帮助信息
    @PostMapping("/add")
    public R add(@RequestBody HelpData helpData){
        //判断是否有数据传入
        if (helpData ==null){
            return R.error();
        }
        //获取Userid
        Long userId= UserHolder.getUser().getId();
        //写入数据
        helpDataService.add(helpData,userId);
        return R.ok();
    }
    //删除帮助信息
    @GetMapping("delete")
    public R delete(@RequestParam("helpId") String helpId){
        helpDataService.delete(helpId);
        return R.ok();
    }
    //更新求助列表
    @PostMapping("/update")
    public R update(@RequestBody HelpData helpData){
        helpDataService.update(helpData);
        return R.ok();
    }
    //获取个人求助列表
    @PostMapping("/pageByUser")
    public R pageByUser(@RequestBody PageParams pageParams){
        Long userId=UserHolder.getUser().getId();
        return R.ok().put("data", helpDataService.pageByUser(pageParams,userId));
    }
    @PostMapping("/getAll")
    public R pageAll(@RequestBody PageParams pageParams){
        return R.ok().put("data",helpDataService.pageAll(pageParams));
    }
    //获取详情
    @GetMapping("/getOne")
    public R getOne(@RequestParam("helpId") Long helpId){
        if(helpId==null){
            throw new EmException("401","参数为空");
        }
        return R.ok().put("data",helpDataService.getOne(helpId));
    }
    //求助评论
    @PostMapping("/addComment")
    public R addComment(@RequestBody HelpComment helpComment){
        helpDataService.addComment(helpComment);
        return R.ok();
    }
    @PostMapping("/updateHot")
    public R update(@RequestParam("helpId") Long helpId){
        if(helpId==null){
            throw new EmException("401","参数为空");
        }
        helpDataService.updateHot(helpId);
        return R.ok();
    }
    //点赞接口
    @GetMapping("/hot")
    public R like(@RequestParam("helpId") Long helpId){
        Integer hot = helpDataService.like(helpId,'0');
        return R.ok().put("data",hot);
    }
    @PostMapping("/hotByUpTime")
    public R hotByUptime(@RequestBody PageParams pageParams){
        return R.ok().put("data",helpDataService.hotByUptime(pageParams));
    }
    @PostMapping("changeHot")
    public R changeHot(@RequestParam("helpId")Long helpId){
        return R.ok().put("data",helpDataService.changeHot(helpId));
    }
}
